﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MINERP.DAL.GoodsDelivery
{
    public class DeliverBackDAL
    {
        private string conStr = ConfigurationManager.ConnectionStrings["sqlCon"].ToString();

        #region 按发货单Id,账单状态查询账单
        /// <summary>
        /// 按发货单Id,账单状态查询账单
        /// </summary>
        /// <param name="deliverId"></param>
        /// <returns></returns>
        public DataTable GetBillsByDeliverId(int deliverId,int status)
        {
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                SqlCommand cmd = new SqlCommand("GetDeliveryBackInfo", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("deliverId", deliverId));
                cmd.Parameters.Add(new SqlParameter("printedStatus", status));
                cmd.Connection = con;
                con.Open();
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
        #endregion

        #region 将发货单状态改为已发送
        /// <summary>
        /// 将发货单状态改为已发送
        /// </summary>
        public void ChangeDeliveryToDeliver(int deliverId,int status)
        {
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                string sql = "update DeliveryInfo set Status=" + status + " where deliverId=" + deliverId;
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
        #endregion

        #region 修改账单的发货信息(新增反馈信息)
        /// <summary>
        /// 修改账单的发货信息(新增反馈信息)
        /// </summary>
        /// <param name="deliverTime"></param>
        /// <param name="arrivalTime"></param>
        /// <param name="deliverObject"></param>
        /// <param name="num"></param>
        /// <param name="stationRemark"></param>
        /// <param name="shopMoney"></param>
        /// <param name="selfMoney"></param>
        /// <param name="otherMoney"></param>
        /// <param name="billRemark"></param>
        public void SaveBillDeliverInfo(List<int> billIds,DateTime deliverTime,DateTime arrivalTime,string deliverObject,string TuoYunDanCode,int num,string stationRemark,double shopMoney,double selfMoney,double otherMoney,string billRemark,string receiveStationRemark)
        {
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                string sql = "update DeliveryInfo set deliverTime='" + deliverTime + "',arrivalTime='" + arrivalTime + "',deliverObject='" + deliverObject + "',TuoYunDanCode='" + TuoYunDanCode + "',num=" + num + ",stationRemark='" + stationRemark + "',shopMoney=" + shopMoney + ",selfMoney=" + selfMoney + ",otherMoney=" + otherMoney + ",Remark='" + billRemark + "',receiveStationRemark='" + receiveStationRemark + "' where billId=";
                SqlCommand cmd = new SqlCommand();
                con.Open();
                cmd.Connection = con;
                foreach (int billId in billIds)
                {
                    sql += billId;
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    sql = sql.Substring(0,sql.LastIndexOf("=") + 1);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
        #endregion

        #region 取消账单
        /// <summary>
        /// 取消账单
        /// </summary>
        /// <param name="billIds"></param>
        public void CancelBills(List<int> billIds)
        {
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                string sql1 = "delete from DeliveryInfo where billId=";
                string sql3 = "update shop_bill set writeintodeliveryinfo=0 where billid=";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                con.Open();
                SqlTransaction tran = con.BeginTransaction();
                cmd.Transaction = tran;
                try
                {
                    foreach (int billId in billIds)
                    {
                        string sql2 = sql1 + billId;
                        cmd.CommandText = sql2;
                        cmd.ExecuteNonQuery();

                        string sql4 = sql3 + billId;
                        cmd.CommandText = sql4;
                        cmd.ExecuteNonQuery();
                    }
                    tran.Commit();
                }
                catch (Exception exx)
                {
                    tran.Rollback();
                    throw exx;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
        #endregion
    }
}
